package com.ryskoo.catering.productmain.service.flower;

import com.ryskoo.catering.productmain.domain.ProductMainPO;
import com.ryskoo.catering.productmain.repository.IProductMainRepository;
import com.ryskoo.common.constant.EurekaServiceName;
import com.ryskoo.framework.core.bean.QueryConditionEnum;
import com.ryskoo.framework.core.bean.QueryMessageDTO;
import com.ryskoo.framework.core.service.BasicServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlowerProductMainServiceImpl extends BasicServiceImpl<ProductMainPO> implements IFlowerProductMainService {
    private static final Logger logger = LogManager.getLogger(EurekaServiceName.CATERING_SERVICE);

    @Autowired
    private IProductMainRepository productRepository;


    @Override
    public List<ProductMainPO> getListFromES(String vendorUuid, String categoryUuid, String attrValueUuid, int queryPage, int pageShow) {
        QueryMessageDTO qm = builderQueryMessage(vendorUuid, categoryUuid, attrValueUuid, queryPage, pageShow);
        return productRepository.getProductInfoFromES(qm, true);
    }

    @Override
    public int getCountFromES(String vendorUuid, String categoryUuid, String attrValueUuid, int queryPage, int pageShow) {
        QueryMessageDTO qm = builderQueryMessage(vendorUuid, categoryUuid, attrValueUuid, queryPage, pageShow);
        return productRepository.getCountFromES(qm);
    }

    private QueryMessageDTO builderQueryMessage(String vendorUuid, String categoryUuid, String attrValueUuid, int queryPage, int pageShow) {
        QueryMessageDTO qm = new QueryMessageDTO();
        if(!StringUtils.isEmpty(vendorUuid)){
            qm.addCondition("vendorUuid", QueryConditionEnum.EQ.name(), vendorUuid);
        }
        if(!StringUtils.isEmpty(categoryUuid)){
            qm.addCondition("categoryUuid", QueryConditionEnum.EQ.name(), categoryUuid);
        }
        if(!StringUtils.isEmpty(attrValueUuid)){
            qm.addCondition("attrValueUuids", QueryConditionEnum.LIKE.name(), attrValueUuid);
        }
        qm.setPageShow(pageShow);
        qm.setQueryPage(queryPage);
        return qm;
    }
}
